package finder

import (
	"encoding/json"
	"go-caipu/pkg/plugins"
	"io"
)

func ReadPluginJSON(reader io.Reader) (plugins.JSONData, error) {
	plugin := plugins.JSONData{}

	if err := json.NewDecoder(reader).Decode(&plugin); err != nil {
		return plugins.JSONData{}, err
	}
	if err := validatePluginJSON(plugin); err != nil {
		return plugins.JSONData{}, err
	}
	if len(plugin.Dependencies.Plugins) == 0 {
		plugin.Dependencies.Plugins = []plugins.Dependency{}
	}
	if plugin.Dependencies.CaiPuDependency == "" {
		plugin.Dependencies.CaiPuDependency = "*"
	}
	return plugin, nil
}
func validatePluginJSON(data plugins.JSONData) error {
	if data.ID == "" || !data.Type.IsValid() {
		return ErrInvalidPluginJSON
	}
	return nil
}
